/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:45
 */

package com.lenovo.mso2o.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.framework.base.BaseDialogFragment;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.MSConstants;
import com.lenovo.mso2o.conf.MSConstants.FragmentType;
import com.lenovo.mso2o.util.Utils;
import com.lenovo.mso2o.view.timepicker.ArrayWheelAdapter;
import com.lenovo.mso2o.view.timepicker.DateUtil;
import com.lenovo.mso2o.view.timepicker.OnWheelChangedListener;
import com.lenovo.mso2o.view.timepicker.WheelView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by lit on 2015/11/26.
 */
public class TimepickerDialogFragment extends BaseDialogFragment implements OnWheelChangedListener, View.OnClickListener {
    public static final String DATE = "date";
    private String[] Hour;
    private String[] monthAndDay;
    private String[] Minute;
    private int d;
    private int h;
    private int min;
    private String today;
    private WheelView day;
    private WheelView minute;
    private WheelView hour;
    private TextView tvDay;
    private TextView tvHour;
    private TextView tvToday;
    private TextView btnCancel;
    private TextView btnSet;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.timepicker_fragment, container);
        day = (WheelView) view.findViewById(R.id.id_day);
        minute = (WheelView) view.findViewById(R.id.id_minute);
        hour = (WheelView) view.findViewById(R.id.id_hour);

        tvDay = (TextView) view.findViewById(R.id.time_day);
        tvHour = (TextView) view.findViewById(R.id.time_hour);
        tvToday = (TextView) view.findViewById(R.id.time_today);
        btnCancel = (TextView) view.findViewById(R.id.cancel);
        btnSet = (TextView) view.findViewById(R.id.set);
        int color = Color.parseColor("#90ffffff");
        day.setShadowColor(color, color, color);
        hour.setShadowColor(color, color, color);
        minute.setShadowColor(color, color, color);
        //monthAndDay = DateUtil.getDayAndWeek();//月日
        monthAndDay = DateUtil.getDayAndWeekAfterToday();
        Hour = DateUtil.getHoursBy24();//时
        Minute = DateUtil.getMinutes();//分

        day.setViewAdapter(new ArrayWheelAdapter<String>(getActivity(), monthAndDay));
        hour.setViewAdapter(new ArrayWheelAdapter<String>(getActivity(), Hour));
        minute.setViewAdapter(new ArrayWheelAdapter<String>(getActivity(), Minute));
        //获取当前时间
        d = DateUtil.getCurrentYearDay();
        h = DateUtil.getCurrentHour();
        min = DateUtil.getMinute();
        today = DateUtil.getYear() + "年" + DateUtil.getMonth() + "月" + DateUtil.getCurrentMonthDay() + "日";
        tvDay.setText(today.trim());
        tvToday.setText("今天");
        tvHour.setText((Hour[h] + ":" + Minute[min]).trim());
        //tvHour.setText((Hour[0] + ":" + Minute[0]).trim());
        //显示当前时间
        //day.setCurrentItem(d-1);
        day.setCurrentItem(0);
        hour.setCurrentItem(h);
        //hour.setCurrentItem(0);
        minute.setCurrentItem(min);
        //minute.setCurrentItem(0);
        day.addChangingListener(this);
        hour.addChangingListener(this);
        minute.addChangingListener(this);
        btnCancel.setOnClickListener(this);
        btnSet.setOnClickListener(this);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
//                        int downY = (int) event.getY() + outHeight * 2;
                        int downY = (int) event.getY();
                        if (isOutDistrict(downY, v)) {
                            dismiss();
                        }
                        break;
                }
                return false;
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == day) {
            int curDayIndex = day.getCurrentItem();
            String curDay = DateUtil.getYear() + "年" + monthAndDay[curDayIndex];
            tvToday.setVisibility(curDayIndex != d - 1 ? View.GONE : View.VISIBLE);
            tvDay.setText(curDayIndex != d - 1 ? curDay.trim() : today);
        } else if (wheel == hour || wheel == minute) {
            int curHourIndex = hour.getCurrentItem();
            String curHour = Hour[curHourIndex];
            int curMinIndex = minute.getCurrentItem();
            String curMin = Minute[curMinIndex];
            tvHour.setText(curHour.trim() + ":" + curMin.trim());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.set:
                Calendar calendar = Calendar.getInstance();
                //Calendar.DAY_OF_YEAR 从１开始
                //calendar.set(Calendar.DAY_OF_YEAR , day.getCurrentItem() + 1);
                calendar.set(Calendar.DAY_OF_YEAR , day.getCurrentItem() + DateUtil.getCurrentYearDay());
                calendar.set(Calendar.HOUR_OF_DAY, hour.getCurrentItem());
                calendar.set(Calendar.MINUTE, minute.getCurrentItem());
                Bundle bundle = new Bundle();
                bundle.putString(DATE, new SimpleDateFormat(MSConstants.DATE_FORMAT_SHORT).format(calendar.getTime()) + ":00");
                if(calendar.before(Calendar.getInstance())){
                    Toast.makeText(getContext(), R.string.time_before_now, Toast.LENGTH_SHORT).show();
                    return;
                }
                mInterface.onRetrieveDialogFragmentData(bundle, FragmentType.DIALOG_TIME_FRAGMENT);
                dismiss();
                break;
        }

    }
}

